<template>
  <div>
    <!-- 面包屑 -->
    <miAn-xie :betGao="betGao"></miAn-xie>
    <!-- 搜索 输入 添加 -->
    <el-card>
      <el-row>
        <el-col :span="10">
          <el-input
            v-model="queryS.query"
            placeholder="请输入内容"
            class="input-with-select"
          >
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>

        <el-col :span="5" :offset="1">
          <el-button type="primary" @click="iao">添加内容</el-button>
        </el-col>
      </el-row>
      <!-- 编辑弹框 -->
     
      <!-- 表格 -->
      <el-table class="users_table" :data="listS" :border="true">
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间">
          <template slot-scope="scope">
            {{scope.row.add_time|time}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="140px">
          <template slot-scope="scope">
            <el-button
              @click="goods_hqu(scope.row.goods_id)"
              type="primary"
              icon="el-icon-edit"
              circle
            >
            </el-button>
            <el-button
              @click="goods_sc(scope.row.goods_id)"
              type="danger"
              icon="el-icon-delete"
              circle
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryS.pagenum"
        :page-sizes="[5, 8, 10, 20]"
        :page-size="queryS.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { goods_goods, goods_delete,goods_get } from "@/http/goods/goods";
export default {
  watch: {
    listS:{
      handler(v){
        if(this.queryS.pagenum>1&&v.length==0){
          this.queryS.pagenum-=1
          this.getCac();
        }
      },
      immediate:true,
      deep:true
    }
  },
  data() {
    return {
      // 面包屑
      betGao:[
        {
          'path':'/home',
          'title':'首页'
        },
        {
          'path':'/goods',
          'title':'商品管理'
        },
        {
          'path':'/goods/add',
          'title':'商品列表'
        },
      ],
      // 获取分页
      queryS: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      /**
       * 表格
       */
      listS: [],
      /**
       * 添加数据
       */
      dialogFormVisible: true,
      Form: {},
    };
  },
  mounted() {
    this.getCac();
  },
  methods: {
    // 获取数据
    async getCac() {
      let res = await goods_goods(this.queryS);
      console.log(res);
      this.listS = res.data.goods;
      this.total = res.data.total;
    },
    // 每页条数
    handleSizeChange(val) {
      //   console.log(`每页 ${val} 条`);
      this.queryS.pagesize = val;
      this.getCac();
    },
    // 当前每页
    handleCurrentChange(val) {
      //   console.log(`当前页: ${val}`);
      this.queryS.pagenum = val;
      this.getCac();
    },
    // 删除
    async goods_sc(id) {
      let res = await goods_delete(id);
      // console.log(res);
      this.getCac();
    },
    async goods_hqu(id){
        let res=await goods_get(id)
        console.log(res);
    },
    // 添加
    iao(){
      this.$router.push('/goods/add')
    }
  },
};
</script>
<style lang="scss" scoped>
.el-table {
  margin: 20px 0;
}
</style>